﻿Imports System.Data.Linq
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Text
Imports Microsoft.Reporting.WinForms

Module mdlGlobals
    Public dt As New dcQLDataContext
    Public loginNhanVien As tbNhanVien

#Region "Undo change"
    Public Sub undoChange()
        ClearUpdateChanges()
        ClearInsertChanges()
        ClearDeleteChanges()
    End Sub

    Public Sub ClearUpdateChanges()
        Dim changes As ChangeSet = dt.GetChangeSet()
        Dim updatedTables As New List(Of ITable)()
        For Each update In changes.Updates
            Dim tbl = dt.GetTable(update.[GetType]())
            If updatedTables.Contains(tbl) Then
                Continue For
            Else
                updatedTables.Add(tbl)
                dt.Refresh(RefreshMode.OverwriteCurrentValues, tbl)
            End If
        Next
    End Sub

    Public Sub ClearInsertChanges()
        For Each insertion In dt.GetChangeSet.Inserts
            dt.GetTable(insertion.[GetType]()).DeleteOnSubmit(insertion)
        Next
    End Sub

    Public Sub ClearDeleteChanges()
        Try
            For Each insertion In dt.GetChangeSet.Deletes
                dt.GetTable(insertion.[GetType]()).InsertOnSubmit(insertion)
            Next
        Catch
        End Try
    End Sub

#End Region

#Region "Bỏ dấu tiếng việt"
    ''' <summary>
    ''' Bỏ dấu tiếng việt
    ''' </summary>
    ''' <remarks></remarks>
    Public Function BoDauTiengViet(accented As String) As String
        Dim regex As New Regex("\p{IsCombiningDiacriticalMarks}+")
        Dim strFormD As String = accented.Normalize(NormalizationForm.FormD)
        Return regex.Replace(strFormD, [String].Empty).Replace("đ"c, "d"c).Replace("Đ"c, "D"c)
    End Function
#End Region

    Public Function DanhSachThamSo(ch As tbCuaHang, tenbaocao As String)
        Dim lstParameter As New ReportParameterCollection
        With lstParameter
            .Add(New ReportParameter("TenCuaHang", ch.TenCuaHangTrenBill))
            .Add(New ReportParameter("ThongTinLienLac", ch.ThongTinLienHeTrenBill))
            .Add(New ReportParameter("NgayIn", Now.ToString("dd/MM/yyyy HH:mm")))
            '.Add(New ReportParameter("TimKiemTheo", ""))
            .Add(New ReportParameter("TenBaoCao", tenbaocao))
            .Add(New ReportParameter("DuongDanAnh", "file:" + My.Settings.DuongDanAnh))
        End With
        Return lstParameter
    End Function

    Public Function DanhSachThamSo(hd As vwHoaDonMoi, Xem As Boolean) As ReportParameterCollection
        Dim ch = dt.tbCuaHangs.Where(Function(s) s.id = hd.idCuaHang).First
        Dim lstParameters As New ReportParameterCollection
        With lstParameters
            .Add(New ReportParameter("HienAnh", My.Settings.HienAnhTrenHoaDon))
            .Add(New ReportParameter("TenPhieu", "Hóa Đơn")) '
            .Add(New ReportParameter("TenCuaHang", ch.TenCuaHangTrenBill)) '
            .Add(New ReportParameter("ThongTinLienHe", ch.ThongTinLienHeTrenBill)) '
            .Add(New ReportParameter("TenKhachHang", hd.TenKhachHang)) '
            .Add(New ReportParameter("SoHoaDon", hd.MaHoaDon.ToString)) '
            .Add(New ReportParameter("NgayLap", hd.NgayLap.ToString("dd/MM/yyyy HH:mm"))) '
            .Add(New ReportParameter("TongTien", String.Format("{0:N0}", hd.TongTienDaGiamGia))) '
            .Add(New ReportParameter("TienChu", Number2Text.Number2Text.Num2Text(hd.TongTienDaGiamGia.ToString))) '
            .Add(New ReportParameter("NhacNho", "")) '
            .Add(New ReportParameter("TenNhanVien", hd.TenNhanVien)) '
            .Add(New ReportParameter("TenPhong", "")) '
            .Add(New ReportParameter("DuongDanAnh", "file:" + My.Settings.DuongDanAnh)) '
            .Add(New ReportParameter("SoPhieu", 1))
            .Add(New ReportParameter("DonViTinh", 1))
        End With
        Return lstParameters
    End Function

    Public Function DanhSachThamSo(hd As vwChiTietPhieuNhap, px As vwChiTietPhieuXuat, nhapxuat As Boolean) As ReportParameterCollection
        'nhapxuat = true -> phiếu nhập
        Dim ch = dt.tbCuaHangs.Where(Function(s) s.id = If(nhapxuat, hd.idCuaHang, px.idCuaHang)).First
        Dim lstParameters As New ReportParameterCollection
        Dim TenPhieu As String = IIf(nhapxuat, "PHIẾU NHẬP", "PHIẾU XUẤT")
        With lstParameters
            .Add(New ReportParameter("TenPhieu", TenPhieu))
            .Add(New ReportParameter("TenCuaHang", ch.TenCuaHangTrenBill))
            .Add(New ReportParameter("ThongTinLienLac", ch.ThongTinLienHeTrenBill))
            .Add(New ReportParameter("SoPhieu", If(nhapxuat, hd.MaPhieu, px.MaPhieu)))
            .Add(New ReportParameter("NgayLap", If(nhapxuat, hd.NgayLap, px.NgayLap).ToString("dd/MM/yyyy HH:mm")))
            .Add(New ReportParameter("TongTien", String.Format("{0:N0}", If(nhapxuat, hd.TongTien, px.TongTien))))
            .Add(New ReportParameter("TienChu", Number2Text.Number2Text.Num2Text(If(nhapxuat, hd.TongTien.ToString, px.TongTien.ToString))))
            .Add(New ReportParameter("GhiChu", If(nhapxuat, hd.GhiChu, px.GhiChu)))
            .Add(New ReportParameter("TenNhanVien", If(nhapxuat, hd.TenNhanVien, px.TenNhanVien)))
            .Add(New ReportParameter("NgayIn", Now.ToString("dd/MM/yyyy HH:mm")))
            .Add(New ReportParameter("DuongDanAnh", "file:" + My.Settings.DuongDanAnh))
        End With
        Return lstParameters
    End Function
End Module